package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import annotation.Log;
import model.TScaleTitleType;
import model.TTitle;
import model.VScaleTitleType;
import model.VTitle;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import persistent.factory.DAOFactory;
import util.toolkit.Constant;
import util.toolkit.Expression;
import util.toolkit.ResponseJSON;

/**
 * 卷类信息处理类
 * @author 徐震霖
 * @since 2019-02-25
 */
@Controller
@RequestMapping(value="/scaletitletype")
public class SysScaleTitleTypeManagementController {

	/**
	 * 卷类信息获取处理类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 卷类信息获取处理类
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "获取卷类信息", 
			operationClassPath = "controller.SysTitleManagementController.getScaleTitleType(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "量表维度管理/卷类信息管理")
	@RequestMapping(value="/getscaletitletype")
	public String getScaleTitleType(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		Expression exp = new Expression();
		
		int currentPage = 1;
		int pageSize = 10;
		
		try {
			String dimensionid = req.getParameter("dimensionid");
			String name = req.getParameter("typename");
			String limit = req.getParameter("limit");
			String page = req.getParameter("page");
			if(page != null && !page.equals("")){
				currentPage = Integer.parseInt(page);
			}
			if(limit != null && !limit.equals("")){
				pageSize = Integer.parseInt(limit);
			}
			if (dimensionid != null && !dimensionid.equals("") && !dimensionid.equals("0")) {
				exp.andEqu("dimensionid", dimensionid, Integer.class);
			}
			if (name != null && !name.equals("")) {
				exp.andLike("name", name, String.class);
			}
			exp.orderByAsc("dimensionName");
			exp.orderByAsc("name");
			List<VScaleTitleType> listsize = 
					DAOFactory.getIScaleTitleTypeDAO().geTScaleTitleTypes(exp.toString());
			List<VScaleTitleType> list = 
					DAOFactory.getIScaleTitleTypeDAO().getScaleTitleTypesByPage(exp.toString(), currentPage, pageSize);
			if (list != null && list.size() > 0) {
				result.put("code", 0);
				result.put("msg", "查询成功");
				result.put("count", listsize.size());
				JSONArray jsonArray = JSONArray.fromObject(list);
				result.element("data", jsonArray);
			}
			else {
				result.put("code", 1);
				result.put("msg", "未获取到卷类信息！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，卷类信息获取失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
		
	}
	
	/**
	 * 获取所有卷类信息（不带分页）
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 * @author djf
	 */
	//TODO 获取所有卷类信息（不带分页）
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "获取所有卷类信息", 
			operationClassPath = "controller.SysTitleManagementController.getAllScaleTitleType(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "量表维度管理/卷类信息管理")
	@RequestMapping(value="/getallscaletitletype")
	public String getAllScaleTitleType(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		Expression exp = new Expression();
		
		try {
			String dimensionid=req.getParameter("dimensionid");
			if(dimensionid!=null && !dimensionid.equals("")){
				exp.andEqu("dimensionid", Integer.parseInt(dimensionid), Integer.class);
			}
			exp.orderByAsc("dimensionName");
			exp.orderByAsc("name");
			
			List<VScaleTitleType> list=new ArrayList<VScaleTitleType>();
			List<VScaleTitleType> listsize = 
					DAOFactory.getIScaleTitleTypeDAO().geTScaleTitleTypes(exp.toString());
			
			if (listsize != null && listsize.size() > 0) {
				for(int i=0;i<listsize.size();i++){
					int scaleTitleTypeid=listsize.get(i).getId();
					Expression exp2=new Expression();
					exp2.andEqu("scaleTitleTypeid", scaleTitleTypeid, Integer.class);
					List<VTitle> titlelist=DAOFactory.getITitleDAO().getTitles(exp2.toString());
					if(titlelist!=null && titlelist.size()>0){
						list.add(listsize.get(i));
					}
				}
				result.put("code", 0);
				result.put("msg", "查询成功");
				result.put("count", list.size());
				JSONArray jsonArray = JSONArray.fromObject(list);
				result.element("data", jsonArray);
			}
			else {
				result.put("code", 1);
				result.put("msg", "未获取到卷类信息！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，卷类信息获取失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}
	
	/**
	 * 下拉框加载卷类信息获取处理类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 下拉框加载卷类信息获取处理类
	@RequestMapping(value="/getScaletitletypebydropdown")
	public String getScaleTitleTypeByDropdown(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		Expression exp = new Expression();
		
		try {
			String dimensionid = req.getParameter("dimensionid");
			if (dimensionid != null && !dimensionid.equals("") && !dimensionid.equals("0")) {
				exp.andEqu("dimensionid", dimensionid, Integer.class);
			}
			exp.orderByAsc("dimensionName");
			exp.orderByAsc("name");
			List<VScaleTitleType> listsize = 
					DAOFactory.getIScaleTitleTypeDAO().geTScaleTitleTypes(exp.toString());
			if (listsize != null && listsize.size() > 0) {
				result.put("code", 0);
				result.put("msg", "查询成功");
				result.put("count", listsize.size());
				JSONArray jsonArray = JSONArray.fromObject(listsize);
				result.element("data", jsonArray);
			}
			else {
				result.put("code", 1);
				result.put("msg", "未获取到卷类信息！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，卷类信息获取失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
		
	}
	
	/**
	 * 卷类信息添加处理类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 卷类信息添加处理类
	@Log(operationType = Constant.OPERTATION_ADD, operationDescribe = "添加一个卷类信息", 
			operationClassPath = "controller.SysTitleManagementController.addScaleTitleType(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "量表维度管理/卷类信息管理")
	@RequestMapping(value="/addscaletitletype")
	public String addScaleTitleType(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		
		try {
			String name = req.getParameter("typename");
			String describe = req.getParameter("describe");
			String titleNumber = req.getParameter("titlenumber");
			String dimensionid = req.getParameter("dimensionid");
			if (name != null && !name.equals("") && describe != null && !describe.equals("") && 
					titleNumber != null && !titleNumber.equals("") && !titleNumber.equals("0") && 
					dimensionid != null && !dimensionid.equals("") && !dimensionid.equals("0")) {
				
				TScaleTitleType scaleTitleType = new TScaleTitleType();
				scaleTitleType.setName(name);
				scaleTitleType.setDimensionid(Integer.parseInt(dimensionid));
				scaleTitleType.setDescribe(describe);
				scaleTitleType.setTitleNumber(Integer.parseInt(titleNumber));
							
				if (DAOFactory.getIScaleTitleTypeDAO().addScaleTitleType(scaleTitleType)) {
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "卷类信息添加成功！");
				}
				else {
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "出现错误，卷类信息添加失败！");
				}
			}
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "必填项不能为空，卷类信息添加失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，卷类信息添加失败，请重试！");
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
		
	}
	
	/**
	 * 修改卷类信息处理类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 修改卷类信息处理类
	@Log(operationType = Constant.OPERTATION_UPDATE, operationDescribe = "修改一个卷类信息", 
			operationClassPath = "controller.SysTitleManagementController.updateScaleTitleType(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "量表维度管理/卷类信息管理")
	@RequestMapping(value="/updatescaletitletype")
	public String updateScaleTitleType(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		
		try {
			String name = req.getParameter("typename");
			String describe = req.getParameter("describe");
			String titleNumber = req.getParameter("titlenumber");
			String dimensionid = req.getParameter("dimensionid");
			String typeid = req.getParameter("typeid");
			
			if (name != null && !name.equals("") && describe != null && !describe.equals("") && 
					titleNumber != null && !titleNumber.equals("") && !titleNumber.equals("0") && 
					dimensionid != null && !dimensionid.equals("") && !dimensionid.equals("0") && 
					typeid != null && !typeid.equals("") && !typeid.equals("0")) {
				
				TScaleTitleType scaleTitleType = DAOFactory.getIScaleTitleTypeDAO().getTScaleTitleType(Integer.parseInt(typeid));
				scaleTitleType.setName(name);
				scaleTitleType.setDimensionid(Integer.parseInt(dimensionid));
				scaleTitleType.setDescribe(describe);
				scaleTitleType.setTitleNumber(Integer.parseInt(titleNumber));
				if (DAOFactory.getIScaleTitleTypeDAO().updateScaleTitleType(scaleTitleType)) {
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "卷类信息修改成功！");
				}
				else {
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "出现错误，卷类信息修改失败！");
				}
			}
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "必填项不能为空，卷类信息修改失败！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，卷类信息修改失败，请重试！");
			e.printStackTrace();
		}
	
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
		
	}
	
	/**
	 * 删除一个卷类信息处理类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 删除一个卷类信息处理类
	@Log(operationType = Constant.OPERTATION_DELETE, operationDescribe = "删除一个卷类信息", 
			operationClassPath = "controller.SysTitleManagementController.updateScaleTitleType(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "量表维度管理/卷类信息管理")
	@RequestMapping(value="/deletescaletitletype")
	public String deleteScaleTitleType(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		
		try {
			String typeid = req.getParameter("typeid");
			
			if (typeid != null && !typeid.equals("") && !typeid.equals("0")) {
				Expression exp = new Expression();
				exp.andEqu("dimensionid", typeid, Integer.class);
				List<VTitle> titles = DAOFactory.getITitleDAO().getTitles(exp.toString());
				if (titles != null && titles.size() > 0) {
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "改卷类下还存在题目信息，请先删除题目信息后重试！");
				}
				else {
					if (DAOFactory.getIScaleTitleTypeDAO().deleteScaleTitleType(Integer.parseInt(typeid))) {
						result.put("code", ResponseJSON.FLAG_SUCC);
						result.put("msg", "卷类信息删除成功！");
					}
					else {
						result.put("code", ResponseJSON.FLAG_FAIL);
						result.put("msg", "出现错误，卷类信息删除失败！");
					}
				}
			}
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "卷类编号不能为空，卷类信息删除失败！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，卷类信息删除失败，请重试！");
			e.printStackTrace();
		}
	
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
		
	}
	
}
